package cn.com.oceansoft.business.mobile.danger.controller;

import cn.com.oceansoft.business.base.entity.Result;
import cn.com.oceansoft.business.danger.entity.Ptcsbz;
import cn.com.oceansoft.business.mobile.danger.entity.CollecterInfo;
import cn.com.oceansoft.business.mobile.danger.service.ICollecterService;
import cn.com.oceansoft.business.mobile.danger.service.IMobilePtcsbzService;
import cn.com.oceansoft.business.mobile.danger.util.PicBzThread;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Created by lenovo on 2018/5/28.
 */
@Controller
@RequestMapping("api/danger/ptcsbz")
public class MobilePtcsbzController {
    @Resource
    private ICollecterService collecterService;
    @Resource
    private IMobilePtcsbzService mobilePtcsbzService;
    @RequestMapping(value = "gotopageWithUser/{pageName}")
    public String gotoMattersPageWithUser(@PathVariable("pageName") String pageName, HttpServletRequest request, Model model) throws IllegalArgumentException, IOException {
        CollecterInfo collecterInfo = collecterService.findByOpenid((String) request.getSession().getAttribute("openId"));
        if(collecterInfo!=null){
            model.addAttribute("collecterInfo",collecterInfo);
            return "mobile/danger/" + pageName;
        }else{
            String url="http://wjw.szgaj.cn/aqyh/api/collecter/collecterInfo";
            return "redirect:"+url;
        }

    }
    @ResponseBody
    @RequestMapping(value = "submit", method = RequestMethod.POST)
    public Result submit(HttpServletRequest request, Ptcsbz ptcsbz) {
        Result result=new Result();
        try{
            mobilePtcsbzService.addPtcsbz(ptcsbz);
            result.setSucc(true);
            result.setMsg("申报成功");
            PicBzThread thread=new PicBzThread(ptcsbz);
            thread.start();
            return result;
        }catch (Exception e){
            e.printStackTrace();
        }
        result.setSucc(false);
        result.setMsg("申报失败");
        return result;
    }
}
